// pages/detail/detail.js
var app = getApp();
var userId;
var url = app.globalData.url;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        URL: app.globalData.url,
        shop_name: '幸福缘',
        menu: [{
            name: '商品',
            action: true,
        }, {
            name: '详情',
            action: false,
        }, {
            name: '评价',
            action: false,
        }],
        img_list:[],
        goods: {},
        containerBlock: [
            'block', 'none', 'none'
        ],
        think: [],
        filter_spec:{},
        isChecked: '0',
        pop: 'none',
        num: 1,
        goodsName: '',
        goodsImg: '',
        current_goods_spec:'',
        hide:false,
        isscroll:true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      userId = wx.getStorageSync('user_id');
      wx.setNavigationBarTitle({
        title: '商品详情'
      })
      // console.log(user_id);
        var that = this;
        // console.log(options.id);
        // app.getUrl(options);
        wx.request({
            url: app.globalData.url +'/api/shop/goods_detail',
            method:'POST',
            data:{
              id: options.id
              // id:457,
            },
            success:function(res){
                // console.log(res.data);
                // res.data.goods.goods_content = strs;
                // for (var i in res.data.filter_spec){
                //     // console.log(res.data.filter_spec[i]);
                //     for (var j in res.data.filter_spec[i]){
                //         res.data.filter_spec[i][j].title = i;
                //         res.data.filter_spec[i][j].select = 0;
                //     }
                // }
                if (res.data.spec_arr.length > 0){
                  that.setData({
                    hide: true,
                    goodsImg: res.data.spec_arr[0]['spec_value'][0]["src"],
                    goodsName: res.data.spec_arr[0]['spec_value'][0]["item"],
                    goods_spec: res.data.spec_arr,
                    current_goods_spec: res.data.spec_arr[0]['spec_value'][0]["item_id"],
                  })
                }
                that.setData({
                    img_list: res.data.goods_images_list,
                    goods: res.data.goods,
                    filter_spec: res.data.filter_spec,
                    goods_original_img: res.data.goods.original_img,
                })
                // console.log(res.data.goods.goods_content);
            }
        });
        wx.request({
            url: app.globalData.url + '/api/shop/ajaxComment',
            method:'POST',
            data: {
                goods_id: options.id,
                commentType:1
            },
            success:function(res){
                // console.log(res.data);
                that.setData({
                    think: res.data.commentlist
                })
            }
        })
        wx.getSystemInfo({
            success: function(res) {
                // console.log(res.model)
                // console.log(res.pixelRatio)
                    // console.log(res.windowWidth)
                    // console.log(res.windowHeight)
                    // console.log(res.language)
                    // console.log(res.version)
            }
        })
        // wx.getLocation({
        //     type: 'wgs84',
        //     success: function(res) {
        //         wx.request({
        //             url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + res.latitude + ',' + res.longitude + '&key=XUBBZ-BY76D-I7644-PQZI3-5BVWV-XBBPN',
        //             success: function(ret) {
        //                 console.log(ret.data.result.address_component);
        //                 that.setData({
        //                     addr: ret.data.result.address_component
        //                 });
        //             }
        //         });
        //         var latitude = res.latitude
        //         var longitude = res.longitude
        //         var speed = res.speed
        //         var accuracy = res.accuracy
        //         console.log(res);
                
        //     }
        // })
    },
    /**
     * 调用全局返回函数 
     */
    returnPage: function(e) {
        app.returnPage(e);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    /**
     * 内容容器变换
     */
    toContainer: function(e) {
        var that = this;
        // console.log(e.currentTarget.dataset.id);
        var index = e.currentTarget.dataset.id;
        for (var i in that.data.menu) {
            if (i == index) {
                that.data.menu[i].action = true;
                that.data.containerBlock[i] = 'block';
            } else {
                that.data.menu[i].action = false;
                that.data.containerBlock[i] = 'none';
            }
        }
        that.setData({
            menu: that.data.menu,
            containerBlock: that.data.containerBlock
        });

    },
    /**
     * 调用全局页面跳转函数
     */
    goToPage: function(e) {
      app.goToTabBar(e);
    },
    /**
     * 购物车操作
     */
    cartAction:function(e){
        console.log(userId);
        console.log('添加购物车');
        console.log(e);
        wx.request({
          url: url + '/api/cart/ajaxAddCart',
          method: 'POST',
          data: {
            user_id: userId,
            goods_id: e.currentTarget.dataset.goods_id,
            goods_num: e.currentTarget.dataset.num,
            goods_spec: e.currentTarget.dataset.current_goods_spec,
          },
          success: function (res) {
            if (res.data.status == 1) {
              if (e.currentTarget.dataset.type == 0) {
                wx.showToast({
                  title: '成功加入购物车',
                  icon: 'success',
                  duration: 2000
                })
              } else {
                wx.switchTab({
                  url: '/pages/cart/cart'
                })
              }
            }
          }
        })
    },
    /**
     * 打开选择面板
     */
    select: function (i) {
      console.log(i)
      if (i.currentTarget.dataset.hide == false){
        // console.log(getCurrentPages());
        wx.request({
          url: url + '/api/cart/ajaxAddCart',
          method: 'POST',
          data: {
            user_id: userId,
            goods_id: i.currentTarget.dataset.goods_id,
            goods_num: i.currentTarget.dataset.num,
          },
          success: function (res) {
            // console.log(res)
            if (res.data.status == 1) {
              if (i.currentTarget.dataset.type == 0){
                wx.showToast({
                  title: '成功加入购物车',
                  icon: 'success',
                  duration: 2000
                })
              }else{
                wx.switchTab({
                  url: '/pages/cart/cart'
                })
              }
            }
          }
        })
      }else{
        this.setData({
          pop: 'block',
          isscroll: false,
        })
      }
    },
    /**
     * 关闭选择面板
     */
    close: function () {
        this.setData({
            pop: 'none',
            isscroll: true,
        })
    },
    /**
     * 规格选择
     */
    classify: function (o) {
        console.log(o);
        this.setData({
            isChecked: o.currentTarget.id,
            goodsName: o.currentTarget.dataset.goodsname,
            goodsImg: o.currentTarget.dataset.goodsimg,
            current_goods_spec: o.currentTarget.dataset.current_goods_spec,
        })
    },
    /**
     * 添加数量
     */
    add: function (n) {
        console.log(n.currentTarget.dataset.num++);
        var count = n.currentTarget.dataset.num++;
        if (count >= n.currentTarget.dataset.maxnum) {
            this.setData({
                num: n.currentTarget.dataset.maxnum,
            })
        } else {
            this.setData({
                num: count,
            })
        }
    },
    /**
     * 减少数量
     */
    dec: function (n) {
        console.log(n.currentTarget.dataset.num--);
        if (n.currentTarget.dataset.num >= 1) {
            var count = n.currentTarget.dataset.num--;
            this.setData({
                num: count,
            })
        }
    },

})